/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.execute.service.impl;

import com.icinfo.cloud.provider.common.enums.CertTypeEnum;
import com.icinfo.cloud.provider.punish.execute.mapper.ExecutePartyInfoMapper;
import com.icinfo.cloud.provider.punish.execute.model.ExecutePartyInfo;
import com.icinfo.cloud.provider.punish.execute.service.IExecutePartyInfoService;
import com.icinfo.cloud.provider.punish.execute.vo.ExecutePartyInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.PartyTypeEnum;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 裁执分离当事人表（个人+企业） execute_party_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月24日
 */
@Service
public class ExecutePartyInfoServiceImpl extends MyBatisServiceSupport implements IExecutePartyInfoService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ExecutePartyInfoServiceImpl.class);

    @Autowired
    private ExecutePartyInfoMapper executePartyInfoMapper;

    @Override
    public List<ExecutePartyInfoVo> getExecutePartyInfo(String executeId) {
        List<ExecutePartyInfoVo> executePartyInfoVos = new ArrayList<>();

        Example queryExample = new Example(ExecutePartyInfo.class);
        queryExample.createCriteria().andEqualTo("executeId", executeId);
        queryExample.setOrderByClause("party_type desc");
        List<ExecutePartyInfo> executePartyInfos = executePartyInfoMapper.selectByExample(queryExample);
        executePartyInfos.forEach(party -> {
            ExecutePartyInfoVo vo = new ExecutePartyInfoVo();
            BeanUtils.copyProperties(party, vo);
            vo.setPartyIdentityTypeName(CertTypeEnum.getValueByCode(party.getPartyIdentityType()));
            if (PartyTypeEnum.LEGAL.getCode().equals(party.getPartyType())){
                vo.setPartyIdentityTypeName("统一社会信用代码");
            }
            vo.setPartyTypeName(PartyTypeEnum.getValueByCode(party.getPartyType()));
            executePartyInfoVos.add(vo);
        });
        return executePartyInfoVos;
    }

    @Override
    public Map<String, String> getExecutePartyIdNameInfo(String executeId) {
        String partyId = "";
        String partyName = "";
        StringBuilder tmpIdStr = new StringBuilder();
        StringBuilder tmpNameStr = new StringBuilder();
        List<ExecutePartyInfoVo> executePartyInfo = getExecutePartyInfo(executeId);
        executePartyInfo.forEach(e -> {
            tmpIdStr.append(e.getId() + ",");
            tmpNameStr.append(e.getPartyName() + ",");
        });

        if (tmpIdStr.length() > 0) {
            partyId = tmpIdStr.substring(0, tmpIdStr.length() - 1);
        }
        if (tmpNameStr.length() > 0) {
            partyName = tmpNameStr.substring(0, tmpNameStr.length() - 1);
        }
        Map<String, String> map = new HashMap<>(2);
        map.put("partyId", partyId);
        map.put("partyName", partyName);
        return map;
    }
}